package com.lge.clock;

import android.content.Context;
import android.text.TextUtils;

import com.lge.clock.worldclock.cities.CityInformation;
import com.lge.clock.worldclock.util.HangulUtils;
import com.lge.clock.util.Log;

/**
 * @author LGE Cappuccino
 */
public class ListMark 
{
	private String mMark;
	private Context mContext;
	public ListMark(Context context)
	{
		mContext = context;
	}
    public void setMarkString(String mark) 
    {
		mMark = mark;
	}
    
    public String getMarkString()
    {
    	return mMark;
    }
    
    public String markString(String data) {
        String resultFormat = null;
        
        if(CityInformation.LANGUAGE_MODE_KOREAN.equals(CityInformation.getLanguageMode()) || "ko_KR".equals(CityInformation.getLanguageMode()))
        {
            resultFormat = markKoreanString(data);
        }
        else
        {
            resultFormat = markEnglishString(data);
        }
        return resultFormat;
    }
    
    public String markEnglishString(String data) {
        int searchIdx = 0;
        String temp[] = null;
        String resultFormat = null;
        String format = "<font color='#8cc839'>%1$s</font>";
        
        if(mMark.equals("")) {
            searchIdx = -1;
        }
        else {
            searchIdx = data.toLowerCase().indexOf(mMark.toLowerCase());
            temp = data.toLowerCase().split(mMark.toLowerCase());
            if((searchIdx > 0) && (temp.length==0)){
            	temp = data.toLowerCase().split("\\"+mMark.toLowerCase()); 
            }
        }
        if(searchIdx < 0) {
            resultFormat = data;
        }
        else {
            int j = 0;
            String tempstring = null;
            String markstring = null;
            for(int i=0; i<temp.length; i++) {
                tempstring = data.substring(j, j+temp[i].length());
                j += temp[i].length();
                if(j+mMark.length() <= data.length()) {
                    markstring = data.substring(j, j + mMark.length());
                }                       
                if(markstring != null)
                {
	                if(resultFormat==null) {
	                    resultFormat = (tempstring+String.format(format, TextUtils.htmlEncode(markstring)));
	                } 
	                else {
	                    if (i+1 == temp.length) {
	                        if(data.substring(data.length()-markstring.length(), data.length()).equals(markstring)) {
	                            resultFormat =  resultFormat + (tempstring+String.format(format, TextUtils.htmlEncode(markstring)));                                    
	                        } 
	                        else {
	                            resultFormat =  resultFormat + tempstring;
	                        }
	                    } 
	                    else {
	                        resultFormat =  resultFormat + (tempstring+String.format(format, TextUtils.htmlEncode(markstring)));
	                    }
	                }
                }
	            else {
	            	Log.e("AlarmClock","markstring is Null");
	            }
                j += mMark.length();
            }
            if(data.equalsIgnoreCase(mMark) && resultFormat == null) {
                resultFormat = String.format(format, TextUtils.htmlEncode(data));
            }
        }
        
        return resultFormat;
    }

    public String markKoreanString(String data) {
        String markString = null;
        String replaceString = null;
        String resultFormat = null;
        String format = mContext.getResources().getString(R.string.style_resource_color);
        
        resultFormat = data;
        
        for(int j = 0; j < (data.length() - mMark.length() + 1); j++)
        {
            markString = HangulUtils.getHangulInitialSound(j , data, mMark);
            
            int index;
            if (markString != null)
            {
            	index = markString.indexOf(mMark);	
            }
            else
            {
            	index = -1;
            }
            
            if(index != -1)
            {
                replaceString = "\\" + data.substring(index + j, index + j +mMark.length());
                resultFormat = resultFormat.replaceAll(replaceString, String.format(format, TextUtils.htmlEncode(replaceString)));
                break;
            }
        }
        
        return resultFormat;
    }
}
